package mapred.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.hadoop.io.BytesWritable;

public class BytesBuilder {

	private ByteArrayOutputStream baos;
	
	public BytesBuilder() {
		baos = new ByteArrayOutputStream();
	}
	
	public void append(byte b) throws IOException {
		this.append(new byte[] { b });
	}
	
	public void append(int i) throws IOException {
		this.append(Bytes.Integer2Bytes(i));
	}
	
	public void append(long l) throws IOException {
		this.append(Bytes.Long2Bytes(l));
	}
	
	public void append(double d) throws IOException {
		this.append(Bytes.Double2Bytes(d));
	}
	
	public void append(byte[] bs) throws IOException {
		baos.write(bs);
	}
	
	public void append(BytesWritable bw) throws IOException {
		this.append(new BytesReader(bw).data());
	}
	
	public byte[] close() throws IOException {
		baos.flush();
		baos.close();
		return baos.toByteArray();
	}
	
}
